home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / wasm223.zip / PRIMES.ASM < prev    next >
Assembly Source File  |  1993-05-04  |  3KB  |  105 lines

  1. ;================================================================
  2. ; Prime Number Generator
  3. ; By Eric Tauck
  4. ;
  5. ; This program generates all 16 bit prime numbers starting with
  6. ; 3.  The numbers are displayed to standard output and may be
  7. ; captured to a file with the the DOS redirection commands.
  8. ;
  9. ; This program makes use of the WASM library files.
  10.  
  11.         INCLUDE 'library\start.asm'
  12.         INCLUDE 'library\memory.asm'
  13.         INCLUDE 'library\message1.asm'
  14.         INCLUDE 'library\message2.asm'
  15.         INCLUDE 'library\case1.asm'
  16.         INCLUDE 'library\convert.asm'
  17.  
  18.         mov     ax, OFFSET banner
  19.         call    MesPutL
  20.  
  21. ;--- set up prime table
  22.  
  23.         mov     ax, 0FFF0H      ;memory to allocate (room for 32760 numbers)
  24.         call    MemAll          ;allocate memory
  25.         jc      main5
  26.  
  27.         mov     si, ax          ;save segment in SI
  28.  
  29.         push    ds
  30.         mov     ds, si
  31.         mov     WORD [0], 3     ;first prime in table
  32.         pop     ds
  33.         mov     ax, 5           ;first candidate prime
  34.         mov     di, 1           ;number of primes in table
  35.  
  36. ;--- check if number in BX has factor in prime table
  37.  
  38. main1   push    ds
  39.         sub     bx, bx          ;starting index into table
  40.         mov     cx, di          ;number of primes so far
  41.         mov     ds, si          ;segment of table
  42.  
  43. main2   push    ax
  44.         sub     dx, dx          ;zero high part of word
  45.         div     WORD [bx]       ;divide
  46.         pop     ax
  47.         or      dx, dx          ;check if remainder
  48.         jz      main3
  49.  
  50.         inc     bx
  51.         inc     bx              ;next number to check
  52.         loop    main2           ;loop back if more
  53.  
  54. ;--- factor not found, must be prime
  55.  
  56.         inc     di              ;increment primes in table
  57.         mov     [bx], ax        ;save at last table entry
  58.  
  59. ;--- next prime candidate
  60.  
  61. main3   pop     ds
  62.         or      cx, cx          ;check if prime
  63.         jnz     main4           ;skip display if not
  64.  
  65.         push    ax
  66.         push    bx
  67.         sub     dx, dx                  ;zero high word
  68.         mov     cx, 10                  ;base
  69.         mov     bx, OFFSET numbuffer    ;place to put string
  70.         call    Num2Str                 ;convert to ASCII string
  71.         mov     ax, OFFSET numbuffer    ;address of string
  72.         call    MesPutL                 ;display to screen
  73.         pop     bx
  74.         pop     ax
  75.  
  76. main4   add     ax, 2           ;next prime candidate
  77.         jnc     main1           ;loop back if no overflow
  78.  
  79. ;--- finished
  80.  
  81.         mov     ax, si          ;allocated memory segment
  82.         call    MemRel          ;release memory
  83.  
  84.         mov     ax, 4C00H       ;exit with no error
  85.         int     21H             ;execute
  86.  
  87. ;--- error, not enough memory
  88.  
  89. main5   mov     ax, OFFSET errmes       ;address of error message
  90.         call    MesErrL                 ;display
  91.         mov     ax, 4CFFH               ;exit with error code
  92.         int     21H                     ;execute
  93.  
  94. ;--- data
  95.  
  96. banner  DB      'Prime Number Generator', 0
  97. errmes  DB      'Not enough memory', 0
  98.  
  99. ;--- uninitialized data
  100.  
  101. numbuffer       LABEL   BYTE
  102.                 ORG     +6
  103.  
  104. END
  105.